package org.jsets.fastboot.quartz.config;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import java.io.IOException;
import java.util.Objects;
import java.util.Properties;
import javax.sql.DataSource;
import org.jsets.fastboot.quartz.component.QuartzJobFactory;
import org.jsets.fastboot.quartz.component.QuartzService;
import org.quartz.Scheduler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Quartz配置
 * 
 * @author wangjie (https://github.com/wj596)
 * @date 2016年6月31日
 *
 */
@Configuration
@EnableConfigurationProperties({ QuartzProperties.class })
@ConditionalOnProperty(prefix = QuartzProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class QuartzAutoConfiguration {

	public static final Logger LOGGER = LoggerFactory.getLogger(QuartzAutoConfiguration.class);

	@Autowired
	private QuartzProperties properties;

	@Bean
	public QuartzJobFactory quartzJobFactory() {
		return new QuartzJobFactory();
	}

	@Bean
	public SchedulerFactoryBean schedulerFactoryBean(QuartzJobFactory quartzJobFactory, ObjectProvider<DataSource> dataSource) {
		SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
		ClassPathResource quartzConfig = new ClassPathResource("/quartz.properties");
		if (quartzConfig.exists()) {
			try {
				LOGGER.info("load properties[{}] from ClassPath", quartzConfig.getFilename());
				PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
				propertiesFactoryBean.setLocation(quartzConfig);
				propertiesFactoryBean.afterPropertiesSet();
				schedulerFactoryBean.setQuartzProperties(propertiesFactoryBean.getObject());
				Object dsConfog = propertiesFactoryBean.getObject().get("org.quartz.jobStore.dataSource");
				if (Objects.isNull(dsConfog)) {
					if (!this.properties.isRamStore()) {
						DataSource ds = dataSource.getObject();
						schedulerFactoryBean.setDataSource(ds);
					}
				}
			} catch (IOException e) {
				LOGGER.error("quartz properties can't load ", e);
			}
		} else {
			Properties properties = new Properties();
			properties.put("org.quartz.threadPool.threadCount", this.properties.getThreadCount());
			properties.put("org.quartz.threadPool.threadPriority", this.properties.getThreadPriority());
			if (!this.properties.isRamStore()) {
				DataSource ds = dataSource.getObject();
				properties.put("org.quartz.jobStore.tablePrefix", "qrtz_");
				schedulerFactoryBean.setDataSource(ds);
			}
			schedulerFactoryBean.setQuartzProperties(properties);
		}
		schedulerFactoryBean.setJobFactory(quartzJobFactory);
		return schedulerFactoryBean;
	}

	@Bean
	public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		return scheduler;
	}

	@Bean
	public QuartzService quartzService() {
		return new QuartzService();
	}
	
	@Bean
	public QuartzRunner quartzRunner() {
		return new QuartzRunner();
	}
}